<script lang="ts">
export default {
  name: 'manage',
};
</script>

<script lang="ts" setup>

import {onMounted, reactive, ref} from 'vue';

import OperateMethodCodeEditor from './components/OperateMethodCodeEditor.vue';
import OperateMethodProfile from './components/OperateMethodProfile.vue';

import {RepoProductEntity} from "@/api/device/product/types";
import {ElMessage} from "element-plus";

import {
  getEntity,
  updateScript,
} from "@/api/device/valuex/task";

const operateMethodProfileRef = ref<any>(); // 这了的变量名称必须和<deviceDecoder-edit-dialog>的ref值一样
const operateMethodCodeEditorRef = ref<any>(); // 这了的变量名称必须和<deviceDecoder-edit-dialog>的ref值一样

const dataState = reactive({
  taskId: 0,
  entity: {} as RepoProductEntity,
});


/**
 * 捕获来自对话框的更新操作
 * @taskParam value
 */
function handleUpdate(value: any) {
  updateScript(dataState.taskId, value).then(() => {
    ElMessage.success('更新成功');
  })
}

/**
 * 刷新页面信息
 */
function handleQuery() {
  getEntity(dataState.taskId).then(({data}) => {
    operateMethodProfileRef.value.initData(data);
    operateMethodCodeEditorRef.value.initData(data);
  });
}

function getHrefParams() {
  let tag = "/device-valuex/device-valuex-script?";
  let start = location.href.indexOf(tag);
  if (start <= 0) {
    return;
  }

  let params = location.href.substring(start + tag.length);
  let taskId = params.split("=").at(1);
  dataState.taskId = Number.parseInt(taskId as string);
}

/**
 * 响应页面安装：页面的初始化工作
 */
onMounted(() => {
  // 从地址栏获得操作方法的ID
  getHrefParams();

  // 向后台查询详细信息
  handleQuery();
});

</script>

<template>
  <div class="app-container">
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>动态解码</el-breadcrumb-item>
        <el-breadcrumb-item>操作列表</el-breadcrumb-item>
        <el-breadcrumb-item>操作详情</el-breadcrumb-item>
      </el-breadcrumb>
      <br>
    </div>


    <!-- 表单头：各种按钮 -->
    <operate-method-profile ref="operateMethodProfileRef"
                            @query="handleQuery"
    />

    <!-- 数据表单：创建/修改 -->
    <operate-method-code-editor ref="operateMethodCodeEditorRef"
                                @update="handleUpdate"
    />

  </div>
</template>
